package com.cn.shiro_jwt_redis.comm.realm;

import com.cn.shiro_jwt_redis.comm.utils.JWTUtil;
import com.cn.shiro_jwt_redis.comm.utils.JwtToken;
import com.cn.shiro_jwt_redis.comm.utils.RedisUtil;
import com.cn.shiro_jwt_redis.mapper.LoginMapper;
import com.cn.shiro_jwt_redis.mapper.MenuMapper;
import com.cn.shiro_jwt_redis.pojo.Login;
import com.cn.shiro_jwt_redis.pojo.Menu;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 张坤鹏
 * @Date: 2021/08/05/9:16
 * @Description:
 */
public class LoginRealm extends AuthorizingRealm {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean supports(AuthenticationToken token) {
        System.out.println("supports方法");
        return token instanceof JwtToken;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("--------授权的方法----------");
        SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
        //获取认证的对象
        System.out.println(principalCollection.getPrimaryPrincipal().toString());
        Login login=(Login) principalCollection.getPrimaryPrincipal();

        String key="menuList"+login.getUsername();
        List<Menu> menuList;
        //获取用户的权限
        //先检测redis里面是否有该数据
        if (redisUtil.hasKey(key)){
            menuList=(List<Menu>) redisUtil.get(key);
            //menuList=redisUtil.lGet(key,0,-1);
            //没有权限数据就去数据库里面拿
        }else {
            menuList=menuMapper.getLoginAllMenu(login.getLoginId());
            redisUtil.set(key,menuList);//把数据存入redis
            //redisUtil.lSet(key,menuList);
        }
        for (Menu menu:menuList) {
            //给用户授权
            simpleAuthorizationInfo.addStringPermission(menu.getMenuName());
        }
        return simpleAuthorizationInfo;
    }



    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("--------认证的方法----------");
        //获取token
        String token=(String)authenticationToken.getCredentials();
        //获取用户名
        String username= JWTUtil.getAccount(token);
        //去数据库查询用户是否存在
        Login login=loginMapper.checkUsername(username);
        if (login==null){
            throw new AuthenticationException("认证失败！");
        }
        String realmName=getName();
        return new SimpleAuthenticationInfo(login,token,realmName);
    }
    
}
